package cn.szzsi.service;

import cn.szzsi.common.kit.ArithUtil;
import cn.szzsi.common.kit.SystemConfigUtil;
import cn.szzsi.common.model.DeliveryAreaRelation;
import cn.szzsi.common.model.DeliveryType;
import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.Product.WeightUnit;
import cn.szzsi.common.model.Receiver;

import java.math.BigDecimal;

public class DeliveryTypeService {
    public static DeliveryTypeService service=new DeliveryTypeService();


    /**
     *
     * @param receiver 收货地址对象
     */
    public DeliveryType getDeliverType(Receiver receiver, String shopId){
        Integer provinceId=receiver.getProvinceId();//省id
        Integer cityId=receiver.getCityId();//市id
        Integer areaId=receiver.getAreaId();//区id
        String allAreaId=provinceId+","+cityId+","+areaId+",0";
        DeliveryAreaRelation deliveryAreaRelation=DeliveryAreaRelation.dao.findFirst("select * from mall_delivery_area_relation where shopId=? and areaId in ("+allAreaId+")",shopId);
        DeliveryType deliveryType =null;
        if(deliveryAreaRelation!=null){//用户的收货地区在商户所设置的模板中
            //1.查看运费模板
            deliveryType= DeliveryType.dao.findById(deliveryAreaRelation.getDeliveryTypeId());
        }else{//用户的收货地区在商户所设置的模板中,给个默认运费模板
            deliveryType= DeliveryType.dao.findById("4773ff1008c74945a9dac21b7f36f864");
        }
        return deliveryType;

    }


    /**
     *
     * @param totalWeight 重量单位：g
     * @param shopId 店铺id
     * @param receiver 收货地址对象
     * @return
     */
    public BigDecimal getDeliveryFee(double totalWeight, String shopId, Receiver receiver){
        Integer provinceId=receiver.getProvinceId();//省id
        Integer cityId=receiver.getCityId();//市id
        Integer areaId=receiver.getAreaId();//区id
        String allAreaId=provinceId+","+cityId+","+areaId+",0";
        DeliveryAreaRelation deliveryAreaRelation=DeliveryAreaRelation.dao.findFirst("select * from mall_delivery_area_relation where shopId=? and areaId in ("+allAreaId+")",shopId);
        if(deliveryAreaRelation!=null){//用户的收货地区在商户所设置的模板中
            //1.查看运费模板
            DeliveryType deliveryType= DeliveryType.dao.findById(deliveryAreaRelation.getDeliveryTypeId());
            //2.计算运费
            BigDecimal deliveryFee=  DeliveryTypeService.service.getDeliveryFee(Double.valueOf(totalWeight),deliveryType);
            return deliveryFee;
        }else{//用户的收货地区不在商户所设置的模板中,将运费置为0
            return new BigDecimal(0.00);
        }
    }


    /**
     * 根据总重量（重量单位：g）和配送模板计算配送费用
     * @return 配送费用
     */
    public BigDecimal getDeliveryFee(double totalWeight,DeliveryType deliveryType) {
        return getDeliveryFee(totalWeight, WeightUnit.g,deliveryType);
    }

    /**
     * 根据总重量（重量单位：g）、单位计算配送费用
     * @return 配送费用
     */
    public BigDecimal getDeliveryFee(double totalWeight, WeightUnit totalWeightUnit, DeliveryType deliveryType) {
        double totalWeightGram = toWeightGram(totalWeight, totalWeightUnit);// 总重量（单位：g）
        double firstWeightGram = toWeightGram(deliveryType.getFirstWeight(), deliveryType.getFirstWeightUnitValue());// 首重量（单位：g）
        double contiuneWeightGram = toWeightGram(deliveryType.getContinueWeight(), deliveryType.getContinueWeightUnitValue());// 续重量（单位：g）
        if(contiuneWeightGram <= 0){
            return BigDecimal.ZERO;
        }
        BigDecimal deliveryFee = new BigDecimal("0");// 配送费用
        if (totalWeightGram <= firstWeightGram) {
            deliveryFee = deliveryType.getFirstWeightPrice();
        } else {
            Double contiuneWeightCount = Math.ceil(ArithUtil.div(ArithUtil.sub(totalWeightGram, firstWeightGram), contiuneWeightGram));
            deliveryFee = deliveryType.getFirstWeightPrice().add(deliveryType.getContinueWeightPrice().multiply(new BigDecimal(contiuneWeightCount.toString())));
        }
        return SystemConfigUtil.getOrderScaleBigDecimal(deliveryFee);
    }

    /**
     * 根据重量、重量单位转换为单位为g的重量值
     *
     * @return 重量值（单位：g）
     */
    public static double toWeightGram(double weight, Product.WeightUnit weightUnit) {
        double weightGram = 0D;// 重量（单位：g）
        if (weightUnit == Product.WeightUnit.g) {
            weightGram = weight;
        } else if (weightUnit == Product.WeightUnit.kg) {
            weightGram = ArithUtil.mul(weight, 1000);
        } else {
            weightGram = ArithUtil.mul(weight, 1000000);
        }
        return weightGram;
    }
}
